package cn.lijiajia3515.cairo.auth.modules.client;

import cn.lijiajia3515.cairo.auth.framework.security.oauth2.server.authorization.client.CairoRegisteredClient;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/client")
public class ClientApi {
	private final ClientService clientService;

	public ClientApi(ClientService clientService) {
		this.clientService = clientService;
	}

	@PostMapping("/find")
	@PreAuthorize("hasAnyAuthority('scp_find:client')")
	public List<CairoRegisteredClient> find() {
		return this.clientService.all();
	}

	@PostMapping("/save")
	@PreAuthorize("hasAnyAuthority('scp_save:client')")
	public RegisteredClient save(@RequestBody RegisteredClient client) {
		return this.clientService.save(client).orElseThrow();
	}

	@PostMapping("/modify")
	@PreAuthorize("hasAnyAuthority('scp_modify:client')")
	public RegisteredClient modify(@RequestBody RegisteredClient client) {
		return this.clientService.modifyByClientId(client).orElseThrow();
	}
}
